<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Common\Model\CaozuologModel;
class ServiceController extends AdminController{
	public function _initialize(){
		parent::_initialize();
	}
	/**
	 * 获取客服中心列表
	 */
	public function getAllService(){
	    $id = I('id');
	    $where = '';
	    if(!empty($id)){
	        $where['id'] = $id;
	    }
	    $r = M('Service')
	    ->where($where)
	    ->select();
	    $data = [
	        'data'=>$r,
	        'status'=>1,
	    ];
	    $this->ajaxReturn($data);
	}
	/**
	 * 新增客服
	 */
	public function addService(){
		$name = I('name');
		if(empty($name)){
			$data = [
			'status'=>-1,
			'msg'=>'缺少昵称'
					];
			$this->ajaxReturn($data);
		}
		$number = I('number');
		if(empty($number)){
			$data = [
			'status'=>-1,
			'msg'=>'请输入账号'
					];
			$this->ajaxReturn($data);
		}
		$type = I('type');
		if(empty($type)){
			$data = [
			'status'=>-1,
			'msg'=>'请选择分类'
					];
			$this->ajaxReturn($data);
		}
		$status = I('status');
		if(!empty($status)){
		    if(!in_array($status, [1,-1])){
		        $data = [
		            'status'=>-1,
		            'msg'=>'参数错误'
		        ];
		        $this->ajaxReturn($data);
		    }
		}
		$pic =I('pic');
		$insert = [
			'name'=>$name,
			'number'=>$number,
			'type'=>$type,
			'status'=>$status,
			'pic'=>$pic,
		];
		$r = M('Service')->add($insert);
		if(!$r){
			$data = [
			'status'=>-1,
			'msg'=>'服务器繁忙'
					];
			$this->ajaxReturn($data);
		}
		$data = [
		'status'=>1,
		'msg'=>'新增成功'
				];
		$CAO_LOG = new CaozuologModel();
		$CAO_LOG->addLog($this->user['username'], '新增客服，账号为'.$number);
		$this->ajaxReturn($data);
	}
	/**
	 * 修改客服
	 */
	public function changeService(){
		$id = I('id');
		if(empty($id)||!M('Service')->where(['id'=>$id])->find()){
			$return = [
			'status'=>-1,
			'msg'=>'未查询到此数据'
					];
			$this->ajaxReturn($return);
		}
		$pic = I('pic');
		$data = [];
		$name = I('name');
		if(!empty($name)){
			$data['name'] = $name;
		}
		$type = I('type');
		if(!empty($type)){
			$data['type'] = $type;
		}
		$number = I('number');
		if(!empty($number)){
			$data['number'] = $number;
		}
		if(!empty($pic)){
			$data['pic'] = $pic;
		}
		$status = I('status');
		if(!empty($status)){
		    if(!in_array($status, [1,-1])){
		        $data = [
		            'status'=>-1,
		            'msg'=>'参数错误'
		        ];
		        $this->ajaxReturn($data);
		    }
		}
		$data['status'] = $status;
		if(count($data)==0){
			$return = [
			'status'=>-1,
			'msg'=>'您没有修改任何信息'
					];
			$this->ajaxReturn($return);
		}
		
		$r = M('Service')->where(['id'=>$id])->save($data);
		if(!$r){
			$data = [
			'status'=>-1,
			'msg'=>'服务器繁忙'
					];
			$this->ajaxReturn($data);
		}
		$data = [
		'status'=>1,
		'msg'=>'修改成功'
				];
		$CAO_LOG = new CaozuologModel();
		$CAO_LOG->addLog($this->user['username'], '修改客服，id为'.$id);
		$this->ajaxReturn($data);
	}
	/**
	 * 删除客服
	 */
	public function deleteService(){
		$id = I('id');
		if(empty($id)||!M('Service')->where(['id'=>$id])->find()){
			$return = [
			'status'=>-1,
			'msg'=>'未查询到此数据'
					];
			$this->ajaxReturn($return);
		}
		$r = M('Service')->delete($id);
		if(!$r){
			$data = [
			'status'=>-1,
			'msg'=>'服务器繁忙'
					];
			$this->ajaxReturn($data);
		}
		$data = [
		'status'=>1,
		'msg'=>'删除成功'
				];
		$CAO_LOG = new CaozuologModel();
		$CAO_LOG->addLog($this->user['username'], '删除客服,id为'.$id);
		$this->ajaxReturn($data);
	}
	
}